package com.qkl.upload.publics.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
public class UploadUtils {

    public static UploadFileDTO parseFile(HttpServletRequest request){
        UploadFileDTO fileDTO = null;
        if( ( (request instanceof MultipartHttpServletRequest))){
            try {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = getMultipartFile(multipartRequest);
                fileDTO = new UploadFileDTO();
                fileDTO.setContent(multipartFile.getBytes());
                fileDTO.setParams(getParams(request));
                fileDTO.setHeaders(getHeaders(request));
                fileDTO.setName(multipartFile.getName());
                fileDTO.setOriginalFilename(multipartFile.getOriginalFilename());
                fileDTO.setSize(multipartFile.getSize());
                fileDTO.setSuffixName(FileHelperUtils.getFileSuffix(multipartFile.getName()));
            }catch (Exception e){}
        }
        return fileDTO;
    }

    private static Map<String,Object> getParams(HttpServletRequest request){
        Map<String,Object> params = new HashMap<>();
        Enumeration<String> stringEnumeration = request.getParameterNames();
        if(null != stringEnumeration ){
            while (stringEnumeration.hasMoreElements()){
                String paramName = stringEnumeration.nextElement();
                params.put(paramName,request.getParameter(paramName));
            }
        }
        return params;
    }

    private static Map<String,String> getHeaders(HttpServletRequest request){
        Map<String,String> headers = new HashMap<>();
        String token = "token";
        String sign = "sign";
        headers.put(token,request.getHeader(token));
        headers.put(sign,request.getHeader(sign));
        return headers;
    }

    private static MultipartFile getMultipartFile(MultipartHttpServletRequest multipartRequest ){
        List<MultipartFile> fileList = getMultipartFileList(multipartRequest);
        if(ObjectUtils.isNotEmpty(fileList)){
            return fileList.get(0);
        }
        return null;
    }

    private static List<MultipartFile> getMultipartFileList(MultipartHttpServletRequest multipartRequest ){
        List<MultipartFile> fileList = new ArrayList<>();
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        if( !ObjectUtils.isEmpty(fileMap)){
            Set<Map.Entry<String, MultipartFile>> fileSet = fileMap.entrySet();
            for(Map.Entry<String, MultipartFile> entry : fileSet){
                MultipartFile file = entry.getValue();
                if(null != file){
                    fileList.add(file);
                }
            }
        }
        return fileList;
    }



}
